package com.offcn.dao.daoimpl;

import com.offcn.dao.BaseDao;
import com.offcn.dao.CourseDao;
import com.offcn.pojo.Course;
import com.offcn.pojo.CourseDetail;
import com.offcn.util.PageUtil;

import java.util.Date;
import java.util.List;

public class CourseDaoImpl extends BaseDao<Course> implements CourseDao {
    @Override
    public Integer queryAllCount(String search) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT COUNT(*) FROM course ");
        //通过传入的模糊查询的条件判断
        if (search != null && search.trim().length() != 0) {
            stringBuilder.append("WHERE courseName LIKE CONCAT('%',?,'%')");
        }
        String sql = stringBuilder.toString();
        int number = getNumber(sql);
        return number;
    }

    //分页查询
    @Override
    public List<Course> queryCourseByPage(PageUtil<Course> pageUtil, String search) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM course ");
        /*根据条件判断*/
        if (search != null && search.trim().length() != 0) {
            stringBuilder.append("WHERE courseName LIKE CONCAT('%'," + search + ",'%') ");
        }
        stringBuilder.append("ORDER BY cid DESC LIMIT ?,?");
        String sql = stringBuilder.toString();
        List<Course> courseList = getBeanList(sql, pageUtil.getStartIndex(), pageUtil.getPageSize());
        return courseList;
    }

    //删除功能
    @Override
    public int deleteCourses(String cids) {
        String sql = "DELETE FROM `course` WHERE `cid` in ( " + cids + ") ";
        int update = update(sql);
        return update;
    }

    //增加功能
    @Override
    public int addCourse(Course course) {
        String sql = "INSERT INTO `course` (\n" +
                "  `courseName`,\n" +
                "  `descs`,\n" +
                "  `courseType`,\n" +
                "  `courseImage`,\n" +
                "  `courseVideo`,\n" +
                "  `coursePrice`,\n" +
                "  `status`,\n" +
                "  `createTime`\n" +
                ") \n" +
                "VALUES\n" +
                "  (\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?\n" +
                "  ) ";
        int add = update(sql, course.getCourseName(), course.getDescs(), course.getCourseType(), course.getCourseImage()
                , course.getCourseVideo(), course.getCoursePrice(), course.getStatus(), new Date());
        return add;
    }

    /*修改功能*/
    @Override
    public int updateCourse(Course course) {
        String sql = "UPDATE \n" +
                "  `edu_system`.`course` \n" +
                "SET\n" +
                "  `courseName` = ?,\n" +
                "  `descs` = ?,\n" +
                "  `courseType` = ?,\n" +
                "  `courseImage` = ?,\n" +
                "  `courseVideo` = ?,\n" +
                "  `coursePrice` = ?,\n" +
                "  `status` = ?,\n" +
                "  `createTime` = ? \n" +
                "WHERE `cid` = ? ";
        int update = update(sql, course.getCourseName(), course.getDescs(), course.getCourseType()
                , course.getCourseImage(), course.getCourseVideo(), course.getCoursePrice()
                , course.getStatus(), new Date(), course.getCid());
        return update;
    }

    //增加明细功能

    @Override
    public int addDetail(CourseDetail c) {
        String sql = "INSERT INTO `coursedetail` (`name`,`type`,`url`,`start_data`,`cid`) VALUES(?,?,?,?,?) ;";
        int update = update(sql, c.getName(), c.getType(), c.getUrl()
                , c.getStart_data(), c.getCid());
        return update;
    }

    @Override
    public Course queryUserById(Integer cid) {
        String sql = "SELECT * FROM course WHERE cid=?";
        Course course = getBean(sql, cid);
        return course;
    }

    //查询所有课程名称
    @Override
    public List<Course> queryAllCourse() {
        String sql = "SELECT * FROM course";
        List<Course> courseList = getBeanList(sql);
        return courseList;
    }

    //分页查询课程
    @Override
    public List<Course> queryByCourseByType(Integer type, Integer limit) {
        String sql = "SELECT * FROM course WHERE courseType=? LIMIT ?";
        List<Course> list = getBeanList(sql, type, limit);
        return list;
    }
    //通过课程名和课程类型来查询课程数量
    @Override
    public int queryCountByType(String courseName, String courseType) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT COUNT(*) FROM course WHERE 1=1 ");
        if (courseName!=null && courseName.trim().length()!=0){
            stringBuilder.append(" and courseName LIKE CONCAT('%','"+ courseName+"','%')");
        }
        if (courseType!=null && courseType.trim().length() !=0){
            stringBuilder.append(" AND courseType="+ Integer.valueOf(courseType));
        }
        String sql = stringBuilder.toString();
        int number = getNumber(sql);
        return number;
    }
    //查询符合条件的分页数据

    @Override
    public List<Course> queryByCourseByTypeLimit(PageUtil<Course> pageUtil, String courseName, String courseType) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM course WHERE 1=1 ");
        if (courseName!=null && courseName.trim().length()!=0){
            stringBuilder.append(" and courseName LIKE CONCAT('%','"+ courseName+"','%')");
        }
        if (courseType!=null && courseType.trim().length() !=0){
            stringBuilder.append(" AND courseType="+ Integer.valueOf(courseType));
        }
        stringBuilder.append(" limit ?,?");
        String sql = stringBuilder.toString();
        List<Course> list = getBeanList(sql, pageUtil.getStartIndex(), pageUtil.getPageSize());
        return list;
    }
    //根据Cid查询课程
    @Override
    public Course queryCourseById(Integer cid) {
        String sql = "SELECT * FROM course WHERE cid = ?";
        Course course = getBean(sql, cid);
        return course;
    }
}
